-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[SR-3463][URLSession] config.httpAdditionalHeaders isn't picked up #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
LGTM |
var result: [String] = [] | ||
var names = Set<String>() | ||
if let hh = currentRequest?.allHTTPHeaderFields { | ||
if httpHeaders != nil { | ||
let hh = httpHeaders as! [String:String] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to use an actual [AnyHashable : Any]
dictionary and turn it's keys and values into strings using String(describing:)
?
This would also require var names = Set<AnyHashable>()
, but AnyHashable
can handle strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@naithar Thank you for reviewing the changes and providing your inputs. I will analyze that and get back to you. Sorry for the delay in response as I was on vacation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@naithar I agree with you that String(describing:) is the preferred way to convert an instance of any type to a string as per the documentation. I think it makes sense if we need to convert only specific elements in the dictionary. Here, we need to process each element in the dictionary and hence converting the dictionary from any type to a string dictionary makes the processing simpler.
We cannot use .lowercased() and .isEmpty methods with the elements of the [AnyHashable : Any] dictionary. Hence, we need to convert them to string before applying those methods.
In order to process the httpHeaders as any type, we need to make following changes.
var names = Set<String>()
if httpHeaders != nil {
// let hh = httpHeaders as! [String:String]
httpHeaders?.forEach {
let name = String(describing:$0.0).lowercased()
guard !names.contains(name) else { return }
names.insert(name)
if String(describing:$0.1).isEmpty {
result.append(String(describing:$0.0) + ";")
} else {
result.append(String(describing:$0.0) + ": " + String(describing:$0.1))
}
}
}
As the element values are already converting to string, I did not change the names variable.
Please let me know your thoughts whether the existing changes are fine or you have any better way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes look ok to me
Fix for https://bugs.swift.org/browse/SR-3463